package com.hbsc.controller;

import com.hbsc.domain.RegisterPo;
import com.hbsc.domain.RegisterVo;
import com.hbsc.service.RegisterService;
import com.hbsc.service.YqTestService;
import com.hbsc.utils.WechartUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xvdong on 2017-10-16.
 */
@Controller
@RequestMapping("/test")
public class TestController extends BaseController {
    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TestController.class);
    @Autowired
    private YqTestService service;
    @Autowired
    private RegisterService registerService;

    @Value("${wx.accesstokenurl}")
    public String accesstokenurl;
    @Value("${wx.appid}")
    public String appid;
    @Value("${wx.secret}")
    public String secret;
    @Autowired
    private WechartUtil util;

    @RequestMapping("/1")
    @ResponseBody
    public String testMethod(HttpSession session){
        //util.getAccessToken();
        session.setAttribute("123","231");
        return "测试内容"+session.getId();
    }

    @RequestMapping("/2")
    @ResponseBody
    public String testDubbo(){
        int i=9/0;
        return service.test();
    }

    @RequestMapping("/3")
    @ResponseBody
    public List<Map> testMybatis(){
        String sql="SELECT * FROM rfq_request LIMIT 100";
        return null;//service.test2(sql);
    }

    @RequestMapping("/1111")
    public String testMethod1111(HttpServletRequest request, Model model){
        Map userInfo=userInfo(request);
        logger.info(userInfo.toString());
        model.addAttribute("userInfo",userInfo);
        return "test/weixin";
    }

    @RequestMapping("/list")
    public String weiXinList(HttpServletRequest request, Model model){
        Map userInfo=userInfo(request);
        logger.info(userInfo.toString());

        List weiXinList=service.weiXinList();
        model.addAttribute("userInfo",userInfo);
        model.addAttribute("weiXinList",weiXinList);
        return "test/weixinList";
    }

    /**
     * 捐赠页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/zf")
    public String testMethodZF(HttpServletRequest request, Model model){
        Map userInfo=userInfo(request);
        logger.info(userInfo.toString());
        model.addAttribute("userInfo",userInfo);
        return "test/weixinJZ";
    }

    /**
     * 年会报名记录信息
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/annualMeeting")
    public String annualMeeting(HttpServletRequest request, Model model){
        Map userInfo=userInfo(request);
        String flag = request.getParameter("flag");//报名标识
        String flag2 = "";//验证码标识
        logger.info("登陆用户信息userInfo：{}",userInfo);
//        userInfo = new HashMap<>();
//        userInfo.put("openid","oV6mtw4iBpTsRSgeE55pHxQCVgpQ");
        model.addAttribute("userInfo",userInfo);
        Long count = registerService.queryRegisterInfoCount((String) userInfo.get("openid"));
        if(StringUtils.isEmpty(flag) && count != null && count>0){
            flag = "alreadyRegistered";
            RegisterVo registerVo = registerService.queryRegisterInfo((String) userInfo.get("openid"));
            model.addAttribute("registerVo",registerVo);

            RegisterVo registerVo2 = service.queryPayRecodeInfo((String) userInfo.get("openid"));
            if(registerVo2 != null && (!StringUtils.isEmpty(registerVo2.getPayRecodeNum()))){
                flag2 = "exist";
            }
        }
        model.addAttribute("flag",flag);
        model.addAttribute("flag2",flag2);
        return "test/annualMeeting";
    }

    /**
     * 提交年会报名记录信息
     * @param request
     * @return
     */
    @RequestMapping("/saveRegisterInfo")
    @ResponseBody
    public Map<String,Object> annualMeetingSub(HttpServletRequest request, RegisterPo po){
        Map<String,Object> returnMap = new HashMap<>();
        returnMap.put("rspcod",200);
        returnMap.put("rspmsg","保存成功！");

        Map userInfo=userInfo(request);
//        userInfo = new HashMap<>();
//        userInfo.put("openid","oV6mtw4iBpTsRSgeE55pHxQCVgpQ");
//        Long count = service.queryPayRecodeInfoCount((String) userInfo.get("openid"));
//        if(count != null && count>0){
//            returnMap.put("rspcod",203);
//            returnMap.put("rspmsg","验证码重复！！！");
//            return returnMap;
//        }
        String checkCode = (String) request.getSession().getAttribute("CheckImgValue");
        try {
            if(StringUtils.isEmpty(po.getOpenid())){
                po.setOpenid((String) userInfo.get("openid"));
            }
            //保存报名信息
            registerService.saveRegisterInfo(po);
            //保存验证码
            service.savePayRecodeInfo(checkCode,(String) userInfo.get("openid"));
            logger.info("保存成功！");
        }catch (Exception e){
            returnMap.put("rspcod",201);
            returnMap.put("rspmsg","保存失败！");
            logger.info("保存失败！原因：{}",e);
        }
        return returnMap;
    }
}
